using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using BearUtil;
using UnityEngine;

/// <summary>
/// 存放 node 公用方法
/// </summary>
public static class NodeMethodUtil
{
    /// <summary>
    /// 获取角色 id
    /// </summary>
    /// <returns></returns>
    public static List<int> GetActorID()
    {
        if (ActorsOverview.Instance.actors == null || ActorsOverview.Instance.actors.Count < 0)
            return null;
            
        List<int> ids = new List<int>();
        foreach (var actor in ActorsOverview.Instance.actors)
        {
            if(actor ==null)
                continue;
                
            ids.Add(actor.role_id);
        }
            
        return ids;
    }
    
    /// <summary>
    /// 获取角色 id
    /// </summary>
    /// <returns></returns>
    public static List<string> GetActorName()
    {
        if (ActorsOverview.Instance.actors == null || ActorsOverview.Instance.actors.Count < 0)
            return null;
            
        List<string> names = new List<string>();
        foreach (var actor in ActorsOverview.Instance.actors)
        {
            if(actor ==null)
                continue;
                
            names.Add(actor.fileName);
        }
            
        return names;
    }

    public static int FindRoleIDbyName(string name)
    {
        if (ActorsOverview.Instance.actors == null || ActorsOverview.Instance.actors.Count < 0)
            return -1;
        
        ActorsOverview.Instance.ClearNull();
        var role = ActorsOverview.Instance.actors.Find(r => r.fileName.Equals(name));
        if (role != null)
            return role.role_id;
        else
            return -1;
    }
    
    public static string FindRoleNamebyID(int id)
    {
        if (ActorsOverview.Instance.actors == null || ActorsOverview.Instance.actors.Count < 0 || id == 0)
            return "";

        ActorsOverview.Instance.ClearNull();
        var role = ActorsOverview.Instance.actors.Find(r => r.role_id.Equals(id));
        if (role != null)
            return role.fileName;
        else
            return "";
    }

    public static string StringOnly(string s)
    {
        if (string.IsNullOrEmpty(s) || !Regex.IsMatch(s, @"^([a-zA-Z_]+)([a-zA-Z0-9])$"))
        {
            return "";
        }

        return s;
    }

    public static string ClassNameOnly(string s)
    {
        if (string.IsNullOrEmpty(s) || !Regex.IsMatch(s, @"^([A-Z])([A-Za-z]+)([a-zA-Z0-9])$"))
        {
            return "";
        }

        return s;
    }
    
    /// <summary>
    /// 创建文件 fileName + extend, 强制重新生成
    /// </summary>
    /// <param name="folderPath"></param>
    /// <param name="fileName"></param>
    public static string Delete(string folderPath, string fileName)
    {
        if (string.IsNullOrEmpty(folderPath) || string.IsNullOrEmpty(fileName))
        {
            Debug.LogError("[NodeMethodUtil] 参数丢失");
            return "";
        }

        var filePath = Application.dataPath.Replace("Assets", folderPath);
        filePath = string.Format("{0}{1}", filePath, fileName);
        if (File.Exists(filePath))
        {
            File.Delete(filePath);
        }

        return filePath;
    }
}
